﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using AnalisisDeRiesgos.Dal;
using System.Web.Mvc;
using System.Globalization;

namespace AnalisisDeRiesgos.Models
{
    public enum Privileges
    {
        Administrator = 0,
        GeneralSecurity = 1,
        AreaSecurity = 2,
        Employe = 3
    }

    public class Utils
    {
        public static CultureInfo culture = new CultureInfo("es-ES");
        public static string CalculateSHA1(object message)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte []msg = Encoding.Default.GetBytes(message.ToString());

            return BitConverter.ToString(sha1.ComputeHash(msg)).ToString().Replace("-", "");
        }

        public static bool HasPrivilege(int CI, Privileges requiredPrivilege)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var resp = from u in entities.users
                        where u.CI == CI && u.enabled
                        select u;

            if (resp.Count() <= 0)
                return false;
            user theUser = resp.First();

            return theUser.privilege <= Convert.ToInt32(requiredPrivilege);
        }

        public static bool MustHavePrivilege(int CI, Privileges requiredPrivilege)
        {
            riskanalysisEntities entities = new riskanalysisEntities();
            var resp = from u in entities.users
                       where u.CI == CI && u.enabled
                       select u;

            if (resp.Count() <= 0)
                return false;
            user theUser = resp.First();

            return theUser.privilege == Convert.ToInt32(requiredPrivilege);
        }

        public static List<string> Frequences = new List<string>();

        public static List<SelectListItem> GetFrequencyList()
        {
            List<SelectListItem> frequencyList = new List<SelectListItem>();
            Frequences = new List<string>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var query = from f in entities.frequences
                        select f;
            foreach (frequence f in query)
                Frequences.Add(f.name);

            for (int i = 0; i < Frequences.Count; i++)
                frequencyList.Add(new SelectListItem
                {
                    Text = Frequences[i],
                    Value = (i + 1).ToString()
                });           
            return frequencyList;
        }

        public static List<string> Affecteds = new List<string>();

        public static List<SelectListItem> GetAffectedList()
        {
            List<SelectListItem>  affectedList = new List<SelectListItem>();
            Affecteds = new List<string>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var query = from a in entities.affecteds
                        select a;
            foreach (affected a in query)
                Affecteds.Add(a.name);

            for (int i = 0; i < Affecteds.Count; i++)
                affectedList.Add(
                    new SelectListItem
                    {
                        Text = Affecteds[i],
                        Value = (i + 1).ToString()
                    });            
            return affectedList;
        }

        public static List<string> ControlExistance = new List<string>();

        public static List<SelectListItem> GetControlExistenceList()
        {
            List<SelectListItem> controlsList = new List<SelectListItem>();
            ControlExistance = new List<string>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var query = from c in entities.controlexistances
                        select c;

            foreach (controlexistance c in query)
                ControlExistance.Add(c.name);

            for (int i = 0; i < ControlExistance.Count; i++)
            {
                controlsList.Add(
                    new SelectListItem
                    {
                        Text = ControlExistance[i],
                        Value = (i + 1).ToString()
                    });
            }            
            return controlsList;
        }

        public static List<string> Severitys = new List<string>();

        public static List<SelectListItem> GetSeverityList()
        {
            List<SelectListItem> severityList = new List<SelectListItem>();
            Severitys = new List<string>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var query = from s in entities.severities
                        select s;
            foreach (severity s in query)
                Severitys.Add(s.name);

            for ( int i = 0; i < Severitys.Count; i++ )
                severityList.Add(
                    new SelectListItem
                    {
                        Text = Severitys[i],
                        Value = (i + 1).ToString()
                    });
            
            return severityList;
        }

        public static Dictionary<int, string> KValuesDictionary = new Dictionary<int,string>();
        public static string[] KValuesText = { "0 No se hizo nada",
                                            "0.4 Se efectuaron medidas y se valido su efectividad",
                                            "0.7 Se ejecutaron medidas y controles requeridos",
                                            "1 Se tiene avances dentro del plazo establecido",
                                            "1.2 No se ejecuto ni programo las actividades en el plazo establecido" };

        public static int[] KValues = { 0, 4, 7, 10, 12 };
        public static List<SelectListItem> GetKValuesList()
        {
            List<SelectListItem> kValuesList = new List<SelectListItem>();
            KValuesDictionary = new Dictionary<int, string>();

            for (int i = 0; i < KValues.Length; i++)
            {
                KValuesDictionary.Add(KValues[i], KValuesText[i]);
                kValuesList.Add(
                    new SelectListItem
                    {
                        Text = KValuesText[i],
                        Value = KValues[i].ToString()
                    });
            }

            return kValuesList;
        }

        public static List<SelectListItem> FillProcessList(int managementId)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var queryProcesses = from p in entities.processes
                                 where p.enabled && p.managementId == managementId
                                 select p;
            if (queryProcesses.Count() < 1)
                return list;

            foreach (process item in queryProcesses)
            {
                list.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.processId.ToString()
                    });
            }

            return list;
        }

        public static List<SelectListItem> FillAreaList(int processId)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var queryProcesses = from a in entities.areas
                                 where a.enabled && a.processId == processId
                                 select a;

            if (queryProcesses.Count() < 1)
                return list;

            foreach (area item in queryProcesses)
            {
                list.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.areaId.ToString()
                    });
            }

            return list;
        }

        public static List<SelectListItem> FillActivityList(int areaId)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var queryProcesses = from a in entities.activities
                                 where a.enabled && a.areaId == areaId
                                 select a;

            if (queryProcesses.Count() < 1)
                return list;

            foreach (activity item in queryProcesses)
            {
                list.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.activityId.ToString()
                    });
            }

            return list;
        }

        public static List<SelectListItem> FillAvailableActionTypesList(int dangerId)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            riskanalysisEntities entities = new riskanalysisEntities();

            var actualActivitys = from a in entities.actions
                                  where a.dangerId == dangerId
                                  select a.actiontype;

            var query = from a in entities.actiontypes
                        select a;
            List<actiontype> listActions = new List<actiontype>(query.ToList());

            foreach (var item in actualActivitys)
            {
                listActions.Remove(item);
            }

            foreach (var item in listActions)
            {
                list.Add(
                    new SelectListItem
                    {
                        Text = item.name,
                        Value = item.actionTypeId.ToString()
                    });
            }

            return list;
        }
    }
}